package top.moxingwang.javawork.common;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

import java.security.*;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.Map;

import javax.crypto.Cipher;



public class ResTest {

    static BASE64Encoder encoder = new BASE64Encoder();
    static BASE64Decoder decoder = new BASE64Decoder();


    private static String src ="my name is cuichongdong";

    public static void main(String[] args) {
        jdkRSA();
    }



    public static Keys getKeys(){
        Keys keys = new Keys();
        KeyPairGenerator keyPairGenerator = null;
        try {
            keyPairGenerator = KeyPairGenerator.getInstance("RSA");
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        keyPairGenerator.initialize(512);
        KeyPair keyPair = keyPairGenerator.generateKeyPair();
        RSAPublicKey rSAPublicKey = (RSAPublicKey)keyPair.getPublic();
        RSAPrivateKey private1 = (RSAPrivateKey)keyPair.getPrivate();
        System.out.println("生成公钥："+encoder.encode(rSAPublicKey.getEncoded()));
        System.out.println("生成私钥："+encoder.encode(private1.getEncoded()));

        keys.publicKey = rSAPublicKey;
        keys.privateKey = private1;

        return keys;
    }


    public static void jdkRSA(){

        //1.初始化秘钥
        try {
            Keys ak = getKeys();
            Keys bk = getKeys();

            //加密结果
            byte[] result = null;


            //2.私钥,公钥--加密

            {
                PKCS8EncodedKeySpec pkcs8EncodedKeySpec = new PKCS8EncodedKeySpec(ak.privateKey.getEncoded());
                KeyFactory instance = KeyFactory.getInstance("RSA");
                KeyFactory keyFactory = instance;
                PrivateKey privateKey = keyFactory.generatePrivate(pkcs8EncodedKeySpec);
                Cipher cipher = Cipher.getInstance("RSA");
                cipher.init(Cipher.ENCRYPT_MODE, privateKey);
                result = cipher.doFinal(src.getBytes());
                System.out.println("私钥加密,公钥解密--加密:"+encoder.encode(result));
            }


            //3.私钥加密,公钥解密---解密
            {
                X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(ak.publicKey.getEncoded());
                KeyFactory keyFactory=KeyFactory.getInstance("RSA");
                PublicKey publicKey = keyFactory.generatePublic(x509EncodedKeySpec);
                Cipher cipher=Cipher.getInstance("RSA");
                cipher.init(Cipher.DECRYPT_MODE, ak.publicKey);
                result=cipher.doFinal(result);
                System.out.println("私钥加密,公钥解密---解密"+new String(result));
            }



        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }




    }

    static class Keys{
        public RSAPublicKey publicKey;
        public RSAPrivateKey privateKey;

    }

}
